define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'consult/index',
                    edit_url: 'consult/edit',
                    del_url: 'consult/del',
                    multi_url: 'consult/multi',
                    table: 'consult'
                },
                showExport: false
            });

            //批量发布
            $(document).on("click", ".btn-issue", function () {
                var data = table.bootstrapTable('getSelections');
                var ids = [];
                if (data.length === 0) {
                    Toastr.error("请选择操作信息");
                    return;
                }

                for (var i = 0; i < data.length; i++) {
                    ids[i] = data[i]['id']
                }

                Layer.confirm(
                    '确认选中的' + ids.length + '条批量处理吗?', {
                        icon: 3,
                        title: __('Warning'),
                        offset: '40%',
                        shadeClose: true
                    },
                    function (index) {
                        Layer.close(index);
                        Backend.api.ajax({
                            url: "consult/checkids",
                            data: {
                                ids: JSON.stringify(ids),
                                is_release : 1
                            }
                        }, function (data, ret) { //成功的回调
                            if (ret.code === 1) {
                                table.bootstrapTable('refresh');
                                Layer.close(index);
                            } else {
                                Layer.close(index);
                                Toastr.error(ret.msg);
                            }
                        }, function (data, ret) { //失败的回调
                            // Toastr.error(ret.msg);
                            Layer.close(index);
                        });
                    }
                );

            });
            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                columns: [
                    [
                        { field: 'state', checkbox: true, },
                        { field: 'id', title: __('Id') ,operate: false },
                        { field: 'name', title: __('Name'), operate: 'like' },
                        { field: 'phone', title: __('Phone'), operate: 'like' },
                        { field: 'address', title: __('Address'), operate: 'like' },
                        { field: 'partner_demand', title: __('Partner demand'), operate: false },
                        { field: 'status', title: __('Status'), formatter: Table.api.formatter.toggle, searchList: { 0: __('Untreated'), 1: __('Processed') }, sortable: true },
                        { field: 'create_time', title: __('create time'),operate: false, addclass: 'datetimerange', datetimeFormat: 'YYYY-MM-DD' },
                        {
                            field: 'operate', title: __('Operate'), table: table,
                            events: Table.api.events.operate,
                            buttons: [],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            this.api.bindevent();
        },
        edit: function () {
            this.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
        }
    };

    return Controller;
});